package com.uflower.order.test;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.uflower.order.constant.enume.OrderStatusEnum;
import com.uflower.order.entity.OrderEntity;
import com.uflower.order.entity.OrderItemEntity;
import com.uflower.order.interceptor.LoginUserInterceptor;
import com.uflower.order.service.OrderItemService;
import com.uflower.order.service.OrderService;
import com.uflower.order.vo.OrderCenterOrderVo;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class CanceledOrdersByUser implements MyOrderVos, InitializingBean {
    @Autowired
    OrderService orderService;
    @Autowired
    OrderItemService orderItemService;

    @Override
    public List<OrderCenterOrderVo> getOrders() {
        Long uid = LoginUserInterceptor.loginUser.get().getId();
        List<OrderEntity> allOrders = orderService.list(new QueryWrapper<OrderEntity>()
                .eq("user_id", uid)
                .eq("status", OrderStatusEnum.CANCLED.getCode()));
        List<OrderCenterOrderVo> orderCenterOrderVos = allOrders.stream().map(order -> {
            OrderCenterOrderVo orderCenterOrderVo = new OrderCenterOrderVo();
            orderCenterOrderVo.setOrderSn(order.getOrderSn());
            orderCenterOrderVo.setCreateTime(order.getCreateTime());
            orderCenterOrderVo.setStatus(order.getStatus());
            orderCenterOrderVo.setFreightAmount(order.getFreightAmount());
            orderCenterOrderVo.setPayAmount(order.getPayAmount());
            orderCenterOrderVo.setPromotionAmount(orderCenterOrderVo.getPromotionAmount());

            List<OrderItemEntity> list = orderItemService.getOrderItemsByOrderSn(order.getOrderSn());
            orderCenterOrderVo.setItems(list);
            return orderCenterOrderVo;
        }).collect(Collectors.toList());
        return orderCenterOrderVos;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        MyOrderVosFactory.register(OrderStatusEnum.CANCLED.getCode(),this);
    }
}
